package br.cin.ufpe.corba.exmaples.complex;

import java.io.IOException;

import br.cin.ufpe.corba.ORB;
import br.cin.ufpe.corba.ObjectRef;
import br.cin.ufpe.corba.interfacerepository.InterfaceDef;
import br.cin.ufpe.corba.namingservice.NamingContextExt;
import br.cin.ufpe.corba.namingservice.NamingContextHelper;
import br.cin.ufpe.corba.namingservice.binding.Binding;
import br.cin.ufpe.corba.namingservice.binding.BindingIterator;
import br.cin.ufpe.corba.namingservice.binding.BindingList;
import br.cin.ufpe.corba.namingservice.binding.BindingType;

public class Client {

	public static void main(String[] args) throws NumberFormatException, IOException {
		ORB orb = ORB.init(1137, "localhost", 1133);
		NamingContextExt nc = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
		
		NamingContextExt calculadorasNC = NamingContextHelper.narrow(nc.resolve_str("calculadoras"));
		
		BindingList list = calculadorasNC.list(10);
		BindingIterator iter = list.iterator();
		while (iter.has_next_one()) {
			Binding bind = iter.next_one();
			if (bind.getType() == BindingType.OBJECT) {
				ObjectRef ref = calculadorasNC.resolve_str(bind.getName().toString());
				System.out.println("Object: " + bind.getName());
				InterfaceDef interfaceDef = ref.get_interface();
				System.out.println("Id: " + interfaceDef.getRepositoryId());
				System.out.println("Sub Ids: ");
				for (String string : interfaceDef.getBaseInterfaces()) {
					System.out.println(string);
				}				
				
				if (ref.is_a(SimpleCalculator.class)) {
					SimpleCalculator calc = SimpleCalculatorHelper.narrow(ref);
					System.out.println("ADD 3 + 4 = " + calc.add(3, 4));
				}
				
				System.out.println("--------------------------------------------------------------");
			}
		}
	}
	
}
